// 如果使用 ES6 导入，请取消注释下一行
// import {readFile, writeFile, writeFileSync, promises as fsPromises} from 'fs';

const {readFile, writeFile, promises: fsPromises} = require('fs');
const host='http://10.188.170.168/'; //桂林
// const host='http://10.188.179.186/'; //南宁
const filePath='../build/web/main.dart.js';
readFile(filePath, 'utf-8', function (err, contents) {
  if (err) {
    console.log(err);
    return;
  }

  let count=0;
  let replaceContent='notosanssc/v36/k3kCo84MPvpLmixcA63oeAL7Iqp5IZJF9bmaG9_FnYxNbPzS5HE.ttf'
  // 区分大小写
  if(contents.indexOf(replaceContent)>-1){
    count++;
  }
  contents = contents.replace(replaceContent, 'k3kCo84MPvpLmixcA63oeAL7Iqp5IZJF9bmaG9_FnYxNbPzS5HE.ttf');
  replaceContent='https://fonts.gstatic.com/s/roboto/v20/'
  if(contents.indexOf(replaceContent)>-1){
    count++;
  }
  contents = contents.replace(replaceContent, host);
  replaceContent='https://fonts.gstatic.com/s/'
  if(contents.indexOf(replaceContent)>-1){
    count++;
  }
  contents = contents.replace(replaceContent, host);

  replaceContent='https://www.gstatic.com/flutter-canvaskit/f40e976bedff57e69e1b3d89a7c2a3c617a03dad/'
  if(contents.indexOf(replaceContent)>-1){
    count++;
  }
  contents = contents.replace(replaceContent, host);

  replaceContent='https://www.gstatic.com/flutter-canvaskit/edd8546116457bdf1c5bdfb13ecb9463d2bb5ed4/'
  if(contents.indexOf(replaceContent)>-1){
    count++;
  }
  contents = contents.replace(replaceContent, host);

  replaceContent='https://www.gstatic.com/flutter-canvaskit/04817c99c9fd4956f27505204f7e344335810aed/'
  if(contents.indexOf(replaceContent)>-1){
    count++;
  }
  contents = contents.replace(replaceContent, host);

  replaceContent='https://www.gstatic.com/flutter-canvaskit/3f3e560236539b7e2702f5ac790b2a4691b32d49/'
  if(contents.indexOf(replaceContent)>-1){
    count++;
  }
  contents = contents.replace(replaceContent, host);

  replaceContent='https://www.gstatic.com/flutter-canvaskit/cb4b5fff73850b2e42bd4de7cb9a4310a78ac40d/'
  if(contents.indexOf(replaceContent)>-1){
    count++;
  }
  contents = contents.replace(replaceContent, host);

  replaceContent='chromium/canvaskit.js'
  if(contents.indexOf(replaceContent)>-1){
    count++;
  }
  contents = contents.replace(replaceContent, 'canvaskit/canvaskit.js');
  
  replaceContent='roboto/v20/KFOmCnqEu92Fr1Me5WZLCzYlKw.ttf'
  if(contents.indexOf(replaceContent)>-1){
    count++;
  }
  contents = contents.replace(replaceContent, 'KFOmCnqEu92Fr1Me5WZLCzYlKw.ttf');

  console.log(`替换${count}处，低于5处需要搜索https://www.gstatic.com/flutter-canvaskit/`)
  writeFile(filePath, contents, 'utf-8', function (err) {
    console.log(err);
  });
});